<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>fetch 文件上传</title>
</head>
<body>
<input type="file" name="file">
<button id="single">单文件上传</button>
<hr>
<input type="file" name="filem" multiple>
<button id="more">多文件上传</button>
<script>
    let single = document.querySelector('#single')
    single.addEventListener('click', function(){
        let data = new FormData();
        data.append('user',"desire")
        data.append('file',document.querySelector('input[name=file]').files[0])
        fetch("fetch", {
            method: "POST",
            body: data
        }).then(res => res.text())
        .then(res => {

            console.log(res);
        })
        .catch(err => {
            console.log(err)
        });
    })

    let btnm = document.querySelectorAll('button')[1]
    btnm.addEventListener('click', function () {

        let data = new FormData()
        data.append("user", "张三丰")
        let fs = document.querySelector('input[name=filem]')
        for (let file of fs.files) {
            data.append('file', file,file.name)
        }
        //同一服务器下
        fetch("/fetchs", {
            method: 'post',
            body: data
        }).then(res => res.text())
            .then(res => {
                console.log(res)
            }).catch(e => {
            console.log(e)
        })
    })
</script>
</body>
</html>
